//
//  DPTubeLineDetail.m
//  DPTubeStatus
//
//  Created by Joel Parsons on 22/11/2011.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import <Twitter/Twitter.h>
#import <MessageUI/MessageUI.h>

#import "DPTubeLineDetailController.h"
#import "DPTubeLine.h"


@implementation DPTubeLineDetailController
@synthesize tweetThisButton;
@synthesize SMSThisButton;
@synthesize nameLabel;
@synthesize statusCodeLabel;
@synthesize statusDetailLabel;
@synthesize tubeLineImageView;

@synthesize tubeLine;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setTubeLineImageView:nil];
    [self setNameLabel:nil];
    [self setStatusCodeLabel:nil];
    [self setStatusDetailLabel:nil];
    [self setTweetThisButton:nil];
    [self setSMSThisButton:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.nameLabel.text = self.tubeLine.name;
    self.statusCodeLabel.text = self.tubeLine.statusCode;
    self.statusDetailLabel.text = self.tubeLine.statusDetails;
    
    NSString * iconPath = [[NSBundle mainBundle] pathForResource:@"LTimage" ofType:@"jpg"];
    UIImage * iconImage = [UIImage imageWithContentsOfFile:iconPath];
    
    self.tubeLineImageView.image = iconImage;
    
    self.title = @"Line Detail";
    
    //its important to check we have this functionality before we present the user the option
    //to tweet or SMS this info.
    if ([TWTweetComposeViewController canSendTweet] == NO) {
        self.tweetThisButton.hidden = YES;
    }
    
    if ([MFMessageComposeViewController canSendText] == NO) {
        self.SMSThisButton.hidden = YES;
    }
    
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)sendSMS:(id)sender {
    MFMessageComposeViewController * messageComposeController = [[MFMessageComposeViewController alloc] init];
    
    NSString * stringToSMS = [NSString stringWithFormat:@"%@ on the %@ line",self.tubeLine.statusCode, self.tubeLine.name];
    messageComposeController.body = stringToSMS;
    
    messageComposeController.messageComposeDelegate = self;
    
    [self presentModalViewController:messageComposeController animated:YES];
}



- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    [self dismissModalViewControllerAnimated:YES];
}



- (IBAction)sendTweet:(id)sender {
    TWTweetComposeViewController * tweetComposeController = [[TWTweetComposeViewController alloc] init];
    
    NSString * stringToTweet = [NSString stringWithFormat:@"%@ on the %@ line",self.tubeLine.statusCode, self.tubeLine.name];
    if ([tweetComposeController setInitialText:stringToTweet]) {
        [self presentModalViewController:tweetComposeController animated:YES];
    }
    
   
    
    
}


















@end
